﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.EvaluationUnitManager
{
    public partial class ProjectLeaderEdit : System.Web.UI.Page
    {
        public string CertificatePicURL
        {
            get
            {
                if (ViewState["CertificatePicURL"] == null)
                {
                    ViewState["CertificatePicURL"] = string.Empty;
                }
                return ViewState["CertificatePicURL"].ToString();
            }
            set
            {
                ViewState["CertificatePicURL"] = value;
            }
        }

        public string AppointmentCardPicURL
        {
            get
            {
                if (ViewState["AppointmentCardPicURL"] == null)
                {
                    ViewState["AppointmentCardPicURL"] = string.Empty;
                }
                return ViewState["AppointmentCardPicURL"].ToString();
            }
            set
            {
                ViewState["AppointmentCardPicURL"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                InitUI();
            }
            this.btnSave.Attributes.Add("onclick", "javascript:return confirm('是否保存?')");
        }

        private void InitUI()
        {
            if (Request.QueryString["ID"]!=null)
            {
                ProjectLeaderData model = ProjectLeader.GetModel(Convert.ToInt32(Request["ID"]));
                this.txtName.Text = model.Name;
                this.txtCredentialsNo.Text = model.CredentialsNo;
                Image1.ImageUrl = model.CertificatePicURL;
                Image2.ImageUrl = model.AppointmentCardPicURL;
                CertificatePicURL = model.CertificatePicURL.Replace("\\", "\\\\");
                AppointmentCardPicURL = model.AppointmentCardPicURL.Replace("\\", "\\\\"); ;
                BindList(model.EIAType);
            }
            else
            {
                BindList(string.Empty);
            }
        }

        private void BindList(string EIATypeList)
        {
            //绑定行业领域
            DataTable eikodl = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("7");
            if (eikodl != null)
            {
                foreach (DataRow dr in eikodl.Rows)
                {
                    ListBox1.Items.Add(new ListItem(dr["name"].ToString(),dr["id"].ToString()));
                }
            }
            if (EIATypeList != string.Empty && EIATypeList != null)
            {
                foreach (string EIAType in EIATypeList.Split(','))
                {
                    ListBox2.Items.Add(new ListItem(XIANGMU.GetEIAType(int.Parse(EIAType)),EIAType));
                    ListBox1.Items.Remove(EIAType);
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] == null && ProjectLeader.WhetherExistence(txtCredentialsNo.Text.Trim()))
            {
                Jscript.Alert("证件编号已存在！", this);
                return;
            }
            if (Request.QueryString["ID"] != null && ProjectLeader.WhetherExistence(txtCredentialsNo.Text.Trim(), int.Parse(Request.QueryString["ID"])))
            {
                Jscript.Alert("证件编号已存在！", this);
                return;
            }
                
            ProjectLeaderData model = new ProjectLeaderData();

            model.Name = txtName.Text.Trim();
            model.CredentialsNo = txtCredentialsNo.Text.Trim();
            model.CertificatePicURL = CertificatePicURL;
            model.AppointmentCardPicURL = AppointmentCardPicURL;
            foreach (ListItem item in ListBox2.Items)
            {
                model.EIAType += item.Value + ',';
            }
            if (model.EIAType != null)
                model.EIAType = model.EIAType.TrimEnd(',');
            if (Request.QueryString["ID"] == null)
            {
                if (Session["evaluationunitID"] != null)
                {
                    model.UnitID = Convert.ToInt32(Session["evaluationunitID"].ToString());
                    if (!ProjectLeader.AddProjectLeader(model))
                        Jscript.Alert("添加失败!", this);
                }
            }
            else
            {
                ProjectLeaderData plData = ProjectLeader.GetModel(Convert.ToInt32(Request["ID"]));
                model.UnitID = plData.UnitID;
                model.ID = Convert.ToInt32(Request.QueryString["ID"]);
                if (!ProjectLeader.UpdateProjectLeader(model))
                    Jscript.Alert("修改失败!", this);
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {     
            CertificatePicURL = GetFileUrl(FileUpload1, Image1, "CertificatePic");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            AppointmentCardPicURL = GetFileUrl(FileUpload2, Image2, "AppointmentCardPic");
        }

        private string GetFileUrl(FileUpload fuLoad, System.Web.UI.WebControls.Image img, string fileType)
        {
            string FileUrl = string.Empty;
            if (txtCredentialsNo.Text.Trim() == string.Empty)
                Jscript.Alert("请先填写证件编号！", this);
            else if (Request.QueryString["ID"] == null && ProjectLeader.WhetherExistence(txtCredentialsNo.Text.Trim()))
                Jscript.Alert("证件编号已存在！", this);
            else if (Request.QueryString["ID"] != null && ProjectLeader.WhetherExistence(txtCredentialsNo.Text.Trim(), int.Parse(Request.QueryString["ID"])))
                Jscript.Alert("证件编号已存在！", this);
            else
            {
                if (fuLoad.HasFile)
                {
                    bool fileOK = false;
                    string fileExtension = Path.GetExtension(fuLoad.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                    if (fileOK)
                    {
                        try
                        {
                            string Suffix = fuLoad.FileName.Split('.')[1];//获取上传文件的后缀
                            string NewName = txtCredentialsNo.Text.Trim() + fileType + "." + Suffix;//重命名上传文件
                            if (!Directory.Exists(Server.MapPath(".") + "\\附件\\"))
                            {
                                Directory.CreateDirectory(Server.MapPath(".") + "\\附件\\");
                            }
                            fuLoad.PostedFile.SaveAs(Server.MapPath(".") + "\\附件\\" + NewName);
                            img.ImageUrl = "附件\\" + NewName;
                            FileUrl = "附件\\\\" + NewName;
                        }
                        catch (Exception)
                        {
                            Jscript.Alert("上传失败！", this);
                        }
                    }
                    else
                    {
                        Jscript.Alert("只能上传图片！", this);
                    }
                }
            }
            return FileUrl;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedItem != null)
            {
                ListItem item = ListBox1.Items.FindByValue(ListBox1.SelectedValue);
                ListBox2.Items.Add(item);
                ListBox1.Items.Remove(item);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedItem != null)
            {
                ListItem item = ListBox2.Items.FindByValue(ListBox2.SelectedValue);
                ListBox1.Items.Add(item);
                ListBox2.Items.Remove(item);
            }
        }        
    }
}
